package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.contract.ActivityResultContracts.TakePicture;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import easier.app.x.CommonCode;
import easier.app.x.result.pic.TakePhoto;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.functions.Function;
import io.reactivex.rxjava3.schedulers.Schedulers;
import java.io.File;
import java.util.List;

public class MainActivity66 extends AppCompatActivity {

    private static final String TAG = "MainActivity66";

    public static final String fileProviderAuthorities = "easier.app.x.fileProvider";

    private ImageView image;
    private ImageView image2;
    private ImageView image3;
    private ImageView image4;
    private ImageView image5;
    private ImageView image6;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity66.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main66 );
        initView();
    }

    private void initView() {

        image = (ImageView) findViewById( R.id.image );
        image.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                File temp = CommonCode.createTempFile( v.getContext(), "temp", ".png" );
                Uri uri = FileProvider.getUriForFile( v.getContext(), fileProviderAuthorities, temp );

                TakePhoto.take(
                    MainActivity66.this.getSupportFragmentManager(),
                    uri,
                    new ActivityResultCallback< Uri >() {
                        @Override
                        public void onActivityResult( Uri result ) {

                            if ( result != null ) {
                                image.setImageURI( result );
                            }
                        }
                    } );
            }
        } );

        image2 = (ImageView) findViewById( R.id.image2 );
        image2.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                TakePhoto.takePreview( MainActivity66.this.getSupportFragmentManager(), new ActivityResultCallback< Bitmap >() {
                    @Override
                    public void onActivityResult( Bitmap result ) {

                        if ( result != null ) {
                            image2.setImageBitmap( result );

                            Observable.just( "" )
                                .doOnNext( new Consumer< String >() {
                                    @Override
                                    public void accept( String file ) throws Throwable {

                                        File ttt = CommonCode.createTempFile( v.getContext(), "ttt", ".png" );
                                        if ( ttt != null ) {
                                            CommonCode.saveBitmap( result, ttt );
                                        }
                                    }
                                } )
                                .subscribeOn( Schedulers.io() )
                                .subscribe();
                        }
                    }
                } );
            }
        } );

        image3 = (ImageView) findViewById( R.id.image3 );

        File tempPic = CommonCode.createTempFile( this, "temp_pic", ".png" );
        Log.d( TAG, "onClick: " + tempPic );
        tempPic = null;//屏蔽掉
        if ( tempPic != null ) {

            Uri uriForFile = FileProvider.getUriForFile( this, fileProviderAuthorities, tempPic );
            Log.d( TAG, "onClick: " + uriForFile );
            if ( uriForFile == null ) {
                return;
            }
            registerForActivityResult( new TakePicture(), new ActivityResultCallback< Boolean >() {
                @Override
                public void onActivityResult( Boolean result ) {

                    Observable.just( result )
                        .map( new Function< Boolean, File >() {
                            @Override
                            public File apply( Boolean aBoolean ) throws Throwable {

                                if ( aBoolean ) {
                                    File tempFile = CommonCode.createTempFile( MainActivity66.this, "self-copy", ".png" );
                                    CommonCode.copyFromUri( MainActivity66.this, uriForFile, tempFile );
                                    return tempFile;
                                }
                                return null;
                            }
                        } )
                        .subscribeOn( Schedulers.io() )
                        .observeOn( AndroidSchedulers.mainThread() )
                        .doOnNext( new Consumer< File >() {
                            @Override
                            public void accept( File file ) throws Throwable {

                                if ( file != null ) {
                                    image3.setImageURI( Uri.fromFile( file ) );
                                } else {
                                    Log.d( TAG, "accept: null file" );
                                }
                            }
                        } )
                        .subscribe();
                }
            } ).launch( uriForFile );
        }

        image4 = (ImageView) findViewById( R.id.image4 );
        image4.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                TakePhoto.fromGallery( MainActivity66.this.getSupportFragmentManager(), new ActivityResultCallback< Uri >() {
                    @Override
                    public void onActivityResult( Uri result ) {

                        image4.setImageURI( result );
                    }
                } );
            }
        } );

        image5 = (ImageView) findViewById( R.id.image5 );
        image5.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

                TakePhoto.fromGalleryMultiple( MainActivity66.this.getSupportFragmentManager(),
                    new ActivityResultCallback< List< Uri > >() {
                        @Override
                        public void onActivityResult( List< Uri > result ) {

                            for ( Uri uri : result ) {
                                Log.d( TAG, "onActivityResult: " + uri );
                            }

                            if ( result.size() > 0 ) {
                                image5.setImageURI( result.get( 0 ) );
                            }
                        }
                    } );
            }
        } );

        image6 = (ImageView) findViewById( R.id.image6 );
        image6.setOnClickListener( new OnClickListener() {
            @Override
            public void onClick( View v ) {

            }
        } );
    }
}